package com.tencent.examples.blueking.mongo.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.tencent.bk.utils.blueking.tools.CommonSugar;
import com.tencent.bk.utils.blueking.tools.ResultUtil.ResultDto;
import com.tencent.bk.utils.blueking.tools.ResultUtil.ResultUtil;
import com.tencent.bk.utils.blueking.tools.StringConverters;
import com.tencent.examples.blueking.extend.QueryParam;
import com.tencent.examples.blueking.mongo.extend.biz.CrudBiz;
import com.tencent.examples.blueking.mongo.model.InstAsst;
import com.tencent.examples.blueking.mongo.model.ObjAsst;
import com.tencent.examples.blueking.mongo.model.ObjAttDes;
import com.tencent.examples.blueking.mongo.output.ObjAttDesOutput;
import com.tencent.examples.blueking.mongo.repository.ObjAttDesRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ObjAttDesService extends CrudBiz<ObjAttDesRepository> {

    @Resource
    private MongoTemplate mongoTemplate;
    @Autowired
    private HostBaseService hostBaseService;
    @Autowired
    private ObjectBaseService objectBaseService;
    @Autowired
    private ObjAsstService objAsstService;
    @Autowired
    private ObjAttDesService objAttDesService;

    @Autowired
    private InstAsstService instAsstService;

    /**
     * 分页查询
     *
     * @param queryParam
     * @return
     */
    public List<ObjAttDes> searchAll(QueryParam queryParam) {
        Query query = new Query();
        query.with(new Sort(new Sort.Order(CommonSugar.getTypedDefault(queryParam.getSort(), Sort.Direction.DESC), CommonSugar.getTypedDefault(queryParam.getSortby(), "id"))));
        Map<String, String> dataMap = queryParam.getSqlMap();
        String bk_obj_id = dataMap.get("bk_obj_id");
        if (StringUtils.isNotEmpty(bk_obj_id)) {
            query.addCriteria(Criteria.where("bk_obj_id").is(bk_obj_id));
        }
        String bk_asst_obj_id_notNull = dataMap.get("bk_asst_obj_id_notNull");
        if (StringUtils.isNotEmpty(bk_asst_obj_id_notNull)) {
            query.addCriteria(Criteria.where("bk_asst_obj_id").ne(null).ne("").exists(true));
        }
        QueryParam.pagination(query, queryParam);//分页设置
        return mongoTemplate.find(query, ObjAttDes.class);
    }

    public List<ObjAttDes> getMdzInfo(String bk_obj_id) {

        List<ObjAttDes> objAttDesList = dao.getMdzInfo(bk_obj_id);

        return objAttDesList;
    }

    /**
     * 原生多表查询
     *
     * @param queryParam
     * @return
     */
    public List<BasicDBObject> getAggregation(QueryParam queryParam) {
        Map<String, String> dataMap = queryParam.getSqlMap();
        LookupOperation lookupAgg = Aggregation.lookup("cc_ObjAsst", "bk_property_id", "bk_obj_id", "objAsst");
        Criteria criteria = new Criteria();
        String bk_asst_obj_id_notNull = dataMap.get("bk_asst_obj_id_notNull");
        if (StringUtils.isNotEmpty(bk_asst_obj_id_notNull)) {
            criteria.and("bk_asst_obj_id").ne(null).ne("").exists(true);
        }
        String bk_host_id_notNull = dataMap.get("bk_host_id_notNull");
        if (StringUtils.isNotEmpty(bk_host_id_notNull)) {
            criteria.and("objAsst.bk_host_id").ne(null).ne("").exists(true);
        }
        String bk_obj_id = dataMap.get("bk_obj_id");
        if (StringUtils.isNotEmpty(bk_obj_id)) {
            criteria.and("bk_obj_id").is(bk_obj_id);
        }
        MatchOperation matchOperation = Aggregation.match(criteria);//lookupAgg一定要在matchOperation的前面 两者顺序颠倒就过滤不了
        Aggregation aggregation = Aggregation.newAggregation(lookupAgg, matchOperation);
        AggregationResults<BasicDBObject> ret = mongoTemplate.aggregate(aggregation, "cc_ObjAttDes", BasicDBObject.class);
        return ret.getMappedResults();
    }

    /**
     * 根据bk_obj_id获取属性--原生sql
     *
     * @param bk_obj_id
     * @return
     */
    public List<BasicDBObject> getObjAttDesAggregationByBkObjId(String bk_obj_id) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("bk_obj_id", bk_obj_id);
        return this.getAggregation(queryParam);
    }

    /**
     * 根据bk_obj_id获取属性
     *
     * @param bk_obj_id
     * @return
     */
    public List<ObjAttDes> getObjAttDesByBkObjId(String bk_obj_id) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("bk_obj_id", bk_obj_id);
        return this.searchAll(queryParam);
    }

    /**
     * //查询枚举类型时，需要传入3个参数，分别为：模型id，属性的id，属性类型
     * 此数据作为
     * 查询ObjDes
     *
     * @param bk_obj_id 模型的bk_obj_id
     * @return
     */
    public JSONArray findEnum(String bk_obj_id, String bk_property_id, String bk_property_type) {
        JSONArray resultArray = new JSONArray();
        //查询枚举类型时，需要传入3个参数，分别为：模型id，属性的id，属性类型
        List<ObjAttDes> objAttDesList = dao.findEnum(bk_obj_id, bk_property_id, bk_property_type);
        if (CollectionUtils.isNotEmpty(objAttDesList)) {
            for (ObjAttDes objAttDes : objAttDesList) {
                List list = Arrays.asList(((ArrayList) objAttDes.getOption()).toArray());
                for (int i = 0; i < list.size(); i++) {
                    JSONObject resultObject = new JSONObject();
                    Object o = list.get(i);
                    resultObject.put("name", (String) ((LinkedHashMap) o).get("name"));
                    resultObject.put("id", (String) ((LinkedHashMap) o).get("id"));
                    resultArray.add(resultObject);
                }

            }
        }
        return resultArray;
    }

    /**
     * 查询枚举类型时，需要传入4个参数，分别为：模型id，属性的id，属性类型,以及数据库中所存的key
     *
     * @param bk_obj_id 模型的bk_obj_id
     * @return
     */
    public String findEnumValue(String bk_obj_id, String bk_property_id, String bk_property_type, String value) {
        //查询枚举类型时，需要传入3个参数，分别为：模型id，属性的id，属性类型
        List<ObjAttDes> objAttDesList = dao.findEnumValue(bk_obj_id, value, bk_property_id);
        if (CollectionUtils.isNotEmpty(objAttDesList)) {
            ObjAttDes objAttDes = objAttDesList.get(0);
            Object[] list = ((ArrayList) objAttDes.getOption()).toArray();
            for (Object o : list) {
                if (value.equals(((LinkedHashMap) o).get("id"))) {
                    return (String) ((LinkedHashMap) o).get("name");
                }
            }
        }
        return "";
    }


    /**
     * 列表数据列名新
     *
     * @param bk_obj_id 模型的bk_obj_id
     * @return
     */
    public String getTableList(String bk_obj_id) {
        List<ObjAttDesOutput> objAttDesOutputList = new ArrayList<>();
        List<ObjAttDes> objAttDesList = this.getObjAttDesByBkObjId(bk_obj_id);
        List<String> list = new ArrayList<String>();
        list.add(bk_obj_id);
        if (CollectionUtils.isNotEmpty(objAttDesList)) {
            objAttDesList.forEach(objAttDes ->
                    objAttDesOutputList.add(new ObjAttDesOutput(objAttDes.getBk_obj_id(), objAttDes.getBk_obj_id() + "," + objAttDes.getBk_property_id(), objAttDes.getBk_property_name(), objAttDes.getIsonly(),
                            objAttDes.getBk_property_type(), objAttDes.getOption())));
            //开始查询对应的关联表是否有数据，有数据开始查询cc_ObjAttDes，以cc_ObjAsst\bk_asst_obj_id为bk_obj_id
            List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(bk_obj_id);
            if (CollectionUtils.isNotEmpty(objAsstList)) {
                objAsstList.forEach(objAsst -> attDesRoll(objAsst, objAttDesOutputList, list));
            }
        }
        return JSONObject.toJSON(ResultUtil.success(objAttDesOutputList)).toString();
    }

    /**
     * 列表数据列名新
     *
     * @param bk_obj_id 模型的bk_obj_id
     * @return
     */
    public Map<String, Object> getTableListOption(String bk_obj_id) {
        Map<String, Object> map = new HashMap<>();
        List<ObjAttDes> objAttDesList = this.getObjAttDesByBkObjId(bk_obj_id);
        List<String> list = new ArrayList<String>();
        list.add(bk_obj_id);
        if (CollectionUtils.isNotEmpty(objAttDesList)) {
            objAttDesList.forEach(objAttDes ->
                    map.put(objAttDes.getBk_obj_id() + "," + objAttDes.getBk_property_id(), objAttDes.getOption()));
            //开始查询对应的关联表是否有数据，有数据开始查询cc_ObjAttDes，以cc_ObjAsst\bk_asst_obj_id为bk_obj_id
            List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(bk_obj_id);
            if (CollectionUtils.isNotEmpty(objAsstList)) {
                objAsstList.forEach(objAsst -> attDesRollOption(objAsst, map, list));
            }
        }
        return map;
    }

    /**
     * 递归所有的属性
     *
     * @return
     */
    private void attDesRollOption(ObjAsst objAsst, Map<String, Object> map, List<String> list) {
        if (objAsst != null && StringUtils.isNotEmpty(objAsst.getBk_asst_obj_id())) {
            List<ObjAttDes> objAttDesList = this.getObjAttDesByBkObjId(objAsst.getBk_asst_obj_id());
            if (CollectionUtils.isNotEmpty(objAttDesList)) {
                if (list.contains(objAsst.getBk_asst_obj_id())) {
                    return;
                }
                list.add(objAsst.getBk_asst_obj_id());
                objAttDesList.forEach(objAttDes ->
                        map.put(objAttDes.getBk_obj_id() + "," + objAttDes.getBk_property_id(), objAttDes.getOption()));
                //开始查询对应的关联表是否有数据，有数据开始查询cc_ObjAttDes，以cc_ObjAsst\bk_asst_obj_id为bk_obj_id
                List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(objAsst.getBk_asst_obj_id());
                if (CollectionUtils.isNotEmpty(objAsstList)) {
                    objAsstList.forEach(objAsst1 -> {
                        if (list.contains(objAsst1.getBk_asst_obj_id())) {
                            return;
                        }
                        list.add(objAsst1.getBk_asst_obj_id());
                        attDesRollOption(objAsst1, map, list);
                    });
                }
            }
        }
    }

    /**
     * 递归所有的属性
     *
     * @param objAttDesOutputList 属性list
     * @return
     */
    private void attDesRoll(ObjAsst objAsst, List<ObjAttDesOutput> objAttDesOutputList, List<String> list) {
        if (objAsst != null && StringUtils.isNotEmpty(objAsst.getBk_asst_obj_id())) {
            List<ObjAttDes> objAttDesList = this.getObjAttDesByBkObjId(objAsst.getBk_asst_obj_id());
            if (CollectionUtils.isNotEmpty(objAttDesList)) {
                if (list.contains(objAsst.getBk_asst_obj_id())) {
                    return;
                }
                list.add(objAsst.getBk_asst_obj_id());
                objAttDesList.forEach(objAttDes ->
                        objAttDesOutputList.add(new ObjAttDesOutput(objAttDes.getBk_obj_id(), objAttDes.getBk_obj_id() + "," + objAttDes.getBk_property_id(), objAttDes.getBk_property_name(), objAttDes.getIsonly(),
                                objAttDes.getBk_property_type(), objAttDes.getOption())));
                //开始查询对应的关联表是否有数据，有数据开始查询cc_ObjAttDes，以cc_ObjAsst\bk_asst_obj_id为bk_obj_id
                List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(objAsst.getBk_asst_obj_id());
                if (CollectionUtils.isNotEmpty(objAsstList)) {
                    objAsstList.forEach(objAsst1 -> {
                        if (list.contains(objAsst1.getBk_asst_obj_id())) {
                            return;
                        }
                        list.add(objAsst1.getBk_asst_obj_id());
                        attDesRoll(objAsst1, objAttDesOutputList, list);
                    });
                }
            }
        }
    }

    /**
     * 列表数据列数据(只查询主表和一层从表信息1)
     * 查询ObjDes
     *
     * @param bk_obj_id           模型的bk_obj_id
     * @param bk_supplier_account 默认为0
     * @param page                頁數
     * @param size                默认为0
     * @return
     */
    public ResultDto getTableListDataNew(String bk_obj_id, String bk_supplier_account, Integer page, Integer size) {
        Map<String, Object> optionMap = getTableListOption(bk_obj_id);
        Pageable pageable = new PageRequest(page == null ? 1 : page, size == null ? 10 : size, Sort.Direction.DESC, "_id");
        Map<String, String> map = getTableListForData(bk_obj_id);
        JSONArray result = new JSONArray();
        List<BasicDBObject> objectList;
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("bk_obj_id", bk_obj_id);
        int moduleType = 0;
        if ("host".equals(bk_obj_id)) {
            objectList = hostBaseService.getAggregation(new QueryParam(), pageable);
            moduleType = 1;
        }else if ("biz".equals(bk_obj_id)){
            objectList = hostBaseService.getAggregation1(new QueryParam(), pageable);
            moduleType = 2;
        } else {
            objectList = objectBaseService.getAggregation(queryParam, pageable);
        }
        int useeIndex = 0;
        if (CollectionUtils.isEmpty(objectList)) {
            return ResultUtil.success();
        }
        for (Object object : objectList) {
            JSONObject jsonObject = new JSONObject();
            Set<String> set = ((BasicDBObject) object).keySet();
            if (moduleType == 0) {
                jsonObject.put(bk_obj_id + ",bk_inst_id", ((BasicDBObject) object).get("bk_inst_id").toString());
            } else if (moduleType == 1) {
                jsonObject.put(bk_obj_id + ",bk_host_id", ((BasicDBObject) object).get("bk_host_id").toString());
            }else if (moduleType == 2){
                jsonObject.put(bk_obj_id + ",bk_biz_id", ((BasicDBObject) object).get("bk_biz_id").toString());
            }
            if (CollectionUtils.isNotEmpty(set)) {
                jsonObject.putAll(getEnum(set, bk_obj_id, map, object, optionMap));
            }
            List<InstAsst> instAsstList;
            if (moduleType == 0) {
                instAsstList = instAsstService.getInstAsstBy(bk_obj_id, StringConverters.ToInteger(((BasicDBObject) object).get("bk_inst_id").toString()));
            } else if (moduleType == 2){
                instAsstList = instAsstService.getInstAsstBy(bk_obj_id, StringConverters.ToInteger(((BasicDBObject) object).get("bk_biz_id").toString()));
            } else {
                instAsstList = instAsstService.getInstAsstBy(bk_obj_id, StringConverters.ToInteger(((BasicDBObject) object).get("bk_host_id").toString()));
            }
            if (CollectionUtils.isNotEmpty(instAsstList)) {
                for (InstAsst instAsst : instAsstList) {
                    QueryParam queryParam2 = new QueryParam();
                    queryParam2.getSqlMap().put("bk_obj_id", instAsst.getBk_asst_obj_id());
                    queryParam2.getSqlMap().put("bk_inst_id", instAsst.getBk_asst_inst_id());
                    List<BasicDBObject> objectList2 = objectBaseService.getAggregation(queryParam2, pageable);
                    if (CollectionUtils.isNotEmpty(objectList2)) {
                        for (Object object2 : objectList2) {
                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("useeIndex", ++useeIndex);
                            Set<String> set2 = ((BasicDBObject) object2).keySet();
                            jsonObject1.put(instAsst.getBk_asst_obj_id() + ",bk_inst_id", ((BasicDBObject) object2).get("bk_inst_id").toString());
                            if (CollectionUtils.isNotEmpty(set2)) {
//                                map.put(instAsst.getBk_asst_obj_id() + ",bk_inst_id", ((BasicDBObject) object2).get("bk_inst_id").toString());
                                jsonObject1.putAll(getEnum(set2, bk_obj_id, map, object2, optionMap));
                            }
                            jsonObject1.putAll(jsonObject);
                            result.add(jsonObject1);
                        }
                    } else {
                        jsonObject.put("useeIndex", ++useeIndex);
                        result.add(jsonObject);
                    }

                }
            } else {
                jsonObject.put("useeIndex", ++useeIndex);
                result.add(jsonObject);
            }
        }
        return ResultUtil.success(result);

    }

    public JSONObject getEnum(Set<String> set, String bk_obj_id, Map<String, String> map, Object object, Map<String, Object> optionMap) {
        JSONObject jsonObject = new JSONObject();
        for (Object aSet : set) {
            if (map.keySet().contains(bk_obj_id + "," + aSet)) {
                String type = map.get(bk_obj_id + "," + aSet);
                if ("enum".equals(type)) {
                    JSONObject enumObject = new JSONObject();
                    String key = StringConverters.objectToString(((BasicDBObject) object).get(aSet));
                    enumObject.put("key", key);
                    if (StringUtils.isNotBlank(key)) {
                        enumObject.put("value", findEnumValue(bk_obj_id, aSet.toString(), "enum", key));
                    } else {
                        enumObject.put("value", "");
                    }
//                    enumObject.put("option", optionMap.get(bk_obj_id + "," + aSet));
                    jsonObject.put(bk_obj_id + "," + aSet, enumObject);
                } else {
                    jsonObject.put(bk_obj_id + "," + aSet, ((BasicDBObject) object).get(aSet));
                }
            }
        }
        return jsonObject;
    }


    /**
     * 列表数据列数据
     * 查询ObjDes
     *
     * @param bk_obj_id           模型的bk_obj_id
     * @param bk_supplier_account 默认为0
     * @return
     */
    public ResultDto getTableListData(String bk_obj_id, String bk_supplier_account) {
        List<String> list = new ArrayList<>();
        Map<String, String> map = getTableListForData(bk_obj_id);
        JSONArray result = new JSONArray();
        List<BasicDBObject> objectList;
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("bk_obj_id", bk_obj_id);
        int moduleType = 0;
        if ("host".equals(bk_obj_id)) {
            objectList = hostBaseService.getAggregation(new QueryParam());
            moduleType = 1;
        } else {
            objectList = objectBaseService.getAggregation(queryParam);
        }
        list.add(bk_obj_id);
        int useeIndex = 0;
        if (CollectionUtils.isNotEmpty(objectList)) {
            for (Object object : objectList) {
                logger.info("开始查询第{}层", useeIndex);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("useeIndex", useeIndex);
//                List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(bk_obj_id);
                List<InstAsst> instAsstList = instAsstService.getInstAsstBy(bk_obj_id, StringConverters.ToInteger(((BasicDBObject) object).get("bk_inst_id").toString()));
                if (CollectionUtils.isNotEmpty(instAsstList)) {
                    for (InstAsst instAsst : instAsstList) {
                        if (!roll(instAsst, jsonObject, map, list, result)) {
                            break;
                        }
                    }
                } else {
                    changeBasicDBObjectAddId(object, bk_obj_id, jsonObject, map, moduleType, result);
                }
                useeIndex++;
            }
        }
        return ResultUtil.success(result);
    }

    public void changeBasicDBObjectAddId(Object object, String bk_obj_id, JSONObject jsonObject, Map<String, String> map, int moduleType, JSONArray result) {
        logger.info("开始放入数据");
        Set<String> set = ((BasicDBObject) object).keySet();
        if (CollectionUtils.isNotEmpty(set)) {
            for (Object aSet : set) {
                JSONObject jsonObject1 = new JSONObject();
                if (moduleType == 0) {
                    jsonObject1.put(bk_obj_id + ",inst_id", ((BasicDBObject) object).get("bk_inst_id").toString());
                } else if (moduleType == 1) {
                    jsonObject1.put(bk_obj_id + ",host_id", ((BasicDBObject) object).get("bk_host_id").toString());
                }
                if (map.keySet().contains(bk_obj_id + "," + aSet)) {
                    String type = map.get(bk_obj_id + "," + aSet);
                    if ("enum".equals(type)) {
                        JSONObject enumObject = new JSONObject();
                        String key = StringConverters.objectToString(((BasicDBObject) object).get(aSet));
                        enumObject.put("key", key);
                        if (StringUtils.isNotBlank(key)) {
                            enumObject.put("value", findEnumValue(bk_obj_id, aSet.toString(), "enum", key));
                        } else {
                            enumObject.put("value", "");
                        }
                        jsonObject1.put(bk_obj_id + "," + aSet, enumObject);
                    } else {
                        jsonObject1.put(bk_obj_id + "," + aSet, ((BasicDBObject) object).get(aSet));
                    }
                }
                jsonObject1.putAll(jsonObject);
            }
        }
        logger.info("结束放入");
    }

    /**
     * 列表数据列名新(筛选数据)
     *
     * @param bk_obj_id 模型的bk_obj_id
     * @return
     */
    public Map<String, String> getTableListForData(String bk_obj_id) {
        Map<String, String> map = new HashMap<>();
        List<ObjAttDes> objAttDesList = this.getObjAttDesByBkObjId(bk_obj_id);
        List<String> list = new ArrayList<String>();
        list.add(bk_obj_id);
        if (CollectionUtils.isNotEmpty(objAttDesList)) {
            objAttDesList.forEach(objAttDes ->
                    map.put(objAttDes.getBk_obj_id() + "," + objAttDes.getBk_property_id(), objAttDes.getBk_property_type()));
            //开始查询对应的关联表是否有数据，有数据开始查询cc_ObjAttDes，以cc_ObjAsst\bk_asst_obj_id为bk_obj_id
            List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(bk_obj_id);
            if (CollectionUtils.isNotEmpty(objAsstList)) {
                objAsstList.forEach(objAsst -> attDesRollForData(objAsst, map, list));
            }
        }
        return map;
    }

    /**
     * 递归所有的属性(筛选数据)
     *
     * @param map
     * @return
     */
    private void attDesRollForData(ObjAsst objAsst, Map<String, String> map, List<String> list) {
        if (objAsst != null && StringUtils.isNotEmpty(objAsst.getBk_asst_obj_id())) {
            List<ObjAttDes> objAttDesList = this.getObjAttDesByBkObjId(objAsst.getBk_asst_obj_id());
            if (CollectionUtils.isNotEmpty(objAttDesList)) {
                if (list.contains(objAsst.getBk_asst_obj_id())) {
                    return;
                }
                list.add(objAsst.getBk_asst_obj_id());
                objAttDesList.forEach(objAttDes -> map.put(objAttDes.getBk_obj_id() + "," + objAttDes.getBk_property_id(), objAttDes.getBk_property_type()));
                //开始查询对应的关联表是否有数据，有数据开始查询cc_ObjAttDes，以cc_ObjAsst\bk_asst_obj_id为bk_obj_id
                List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(objAsst.getBk_asst_obj_id());
                if (CollectionUtils.isNotEmpty(objAsstList)) {
                    objAsstList.forEach(objAsst1 -> {
                        attDesRollForData(objAsst1, map, list);
                    });
                }
            }
        }
    }


    private Boolean roll(InstAsst instAsst, JSONObject jsonObject, Map<String, String> map, List<String> list, JSONArray result) {
        if (instAsst != null && StringUtils.isNotBlank(instAsst.getBk_asst_obj_id())) {
            if (list.contains(instAsst.getBk_asst_obj_id())) {
                return false;
            }
            list.add(instAsst.getBk_asst_obj_id());
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("bk_obj_id", instAsst.getBk_asst_obj_id());
            logger.info("开始查询子表数据{}", instAsst.getBk_asst_obj_id());
            List<BasicDBObject> objects = objectBaseService.getAggregation(queryParam);
            logger.info("查询结束子表数据{}", instAsst.getBk_asst_obj_id());
            if (CollectionUtils.isNotEmpty(objects)) {
                for (Object object : objects) {
                    logger.info("查询关联数据{}", instAsst.getBk_asst_obj_id());
                    List<InstAsst> instAsstList = instAsstService.getInstAsstBy(instAsst.getBk_asst_obj_id(), StringConverters.ToInteger(instAsst.getBk_asst_inst_id()));
//                    List<ObjAsst> objAsstList = objAsstService.getObjAsstListBybk_obj_id(instAsst.getBk_asst_obj_id());
                    logger.info("查询结束关联{}", instAsst.getBk_asst_obj_id());
                    if (CollectionUtils.isNotEmpty(instAsstList)) {
                        instAsstList.forEach(instAsst1 -> {
                            roll(instAsst1, jsonObject, map, list, result);
                        });
                    } else {
                        changeBasicDBObjectAddId(object, instAsst.getBk_asst_obj_id(), jsonObject, map, 0, result);
                    }
                }
            }
        }
        return true;
    }

}
